<?php
class Admin_administrator_Controller extends Template_Controller {
	
	public $search;
	public $template = 'admin/index';
	
    public function __construct()
    {
        parent::__construct();
        
        $this->search = array('keyword' => '','page' => 0);
    }
    
	public function __call($method, $arguments)
	{
		// Disable auto-rendering
		$this->auto_render = FALSE;
	}
	
	private function _get_submit()
	{
		if($this->session->get('error_msg'))
			$this->template->error_msg = $this->session->get('error_msg');
		if($this->session->get('warning_msg'))
			$this->template->warning_msg = $this->session->get('warning_msg');
		if($this->session->get('success_msg'))
			$this->template->success_msg = $this->session->get('success_msg');
		if($this->session->get('info_msg'))
			$this->template->info_msg = $this->session->get('info_msg');
		if($this->session->get('frm'))			
			$this->template->content->mr = $this->session->get('frm');
	}
	
    public function index()
    {	
        $this->showlist();
        
        $this->_get_submit();
    }
    
    public function showlist()
    {
    	$this->template->content = new View('admin_administrator/list');
    		
		//Assign
		$this->template->content->keyword = $this->search['keyword'];
		
		$model = new Administrator_Model();
		$this->where_sql();
		$mlist = $model->get();
		
		//Pagination
    	$this->pagination = new Pagination(array(
    		'base_url'    => 'admin_administrator/search/',
		    'uri_segment'    => 'page',
		    'total_items'    => count($mlist),
		    'items_per_page' => $this->site['site_num_line2'],
		    'style'          => 'digg',
		));		
		$this->db->limit($this->pagination->items_per_page,$this->pagination->sql_offset);
    	
    	
    	$this->where_sql();
    	$mlist = $model->get();
    	$this->template->content->mlist = $mlist;
    	$this->_get_submit();
    }
    
    public function where_sql()
    {
    	if($this->search['keyword'])
    		$this->db->like('admin_name',$this->search['keyword']);
    }
    
    public function search()
    {    	
    	//Get    	
    	if($this->session->get('sess_search')){			
			$this->search = $this->session->get('sess_search');
		}
		//Keyword
		$keyword = $this->input->post('txt_keyword');
		if(isset($keyword)){    		
			$this->search['keyword'] = $keyword;
		}
		
		//Page
		$page = $this->uri->segment('page');
		if($page)
			$this->search['page'] = $page;
			
		//Set
		$this->session->set_flash('sess_search',$this->search);
		
		$this->showlist();
    }
    
    public function create()
    {
    	$this->template->content = new View('admin_administrator/frm');
    	
    	$this->_get_submit();
    }
    
    public function edit($id)
    {	
    	$this->template->content = new View('admin_administrator/frm');
    	
    	$this->_get_submit();
    	
    	$this->template->content->mr = ORM::factory('administrator')->find($id)->as_array();
    }
    
    private function _set_form($form)
    {	
		$record['admin_name'] = $form['txt_username'];
		if(!$form['txt_pass']){
           	unset($record['admin_pass']);
        } else {
           	$record['admin_pass'] = md5($form['txt_pass']);
        }
		$record['admin_email'] = $form['txt_email'];
		$record['admin_status'] = $form['sel_status'];		
		return $record;
	}
    
    private function _get_record()
    {
    	$form = array
	    (
	        'txt_username' => '',
	        'txt_pass' => '',
	        'txt_email' => '',
	        'sel_status' => '',
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_username','required');
			$post->add_rules('txt_email','email');
			
			$form = arr::overwrite($form, $post->as_array());
			$form = $this->_set_form($form);
			if($post->validate())
 			{
 				return $form; 				
			} else {
				$this->session->set_flash('frm',$form);
				$errors = arr::overwrite($errors, $post->errors('administrator_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);
				
				$hd_id = $this->input->post('hd_id');
				if($hd_id)
					url::redirect('admin_administrator/edit/'.$hd_id);
				else
					url::redirect('admin_administrator/create');
				die();
			}
        }
    }
    
    public function save()
    {
    	$hd_id = $this->input->post('hd_id');
    	
    	$record = $this->_get_record();
		
		if($record)
		{
			if(!$hd_id){
				$query = $this->db->insert('admin', $record);
				$hd_id = $query->insert_id();
			} else {
				$query = $this->db->update('admin', $record, array('admin_id' => $hd_id));
			}	
			
			if(!$this->input->post('hd_id'))
				$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_add'));
			else
				$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_save'));
		}
		url::redirect('admin_administrator/edit/'.$hd_id);
		die();    	
    }
    
    public function saveall()
    {
    	$arr_id = $this->input->post('chk_id');
		
		if(is_array($arr_id)){
			$sel_action = $this->input->post('sel_action');
		
			if($sel_action == 'delete')	{
				$id = ORM::factory('user')->delete_all($arr_id);
				if($id)
					$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_del'));
			} elseif($sel_action == 'block') {
				foreach($arr_id as $arr => $id){
					$result = ORM::factory('administrator',$id);
			    	$result->admin_status = 0;
			    	$id = $result->save();
		    	}
		    	if($id)
					$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_save'));
			} elseif($sel_action == 'active')
			{
				foreach($arr_id as $arr => $id){
					$result = ORM::factory('administrator',$id);
			    	$result->admin_status = 1;
			    	$id = $result->save();
		    	}
		    	if($id)
					$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_save'));
			}			
		} else {
			$this->session->set_flash('warning_msg',Kohana::lang('errormsg_lang.msg_data_error'));
		}		

		url::redirect('admin_administrator/search');		
		die();
    }
    
    public function delete($id)
    {
    	$status = $this->db->delete('admin',array('admin_id' => $id));
    	if(count($status)>0)
		{
			$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_del'));
		}
    	url::redirect('admin_administrator/search');
    	die();
    }
    
    public function setstatus($id,$status)
    {
    	$status = $this->db->update('admin',array('admin_status' => $status),array('admin_id' => $id));
    	if(count($status)>0)
		{
			$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_status_change'));
		}
    	url::redirect('admin_administrator/search'); 
    	die();
    }
}
?>